﻿/*
Created By:     wudongkuo
Created When:   2012-3-31
Created Whys:   UKnow 2  register
*/
//{
$(function () {
    $('#Provicelist').change(function () {
        GetCity();
    });
   

});
//}
//{
//验证邮箱格式
function EmailVate(strValue ,fnErro,fnSucess) {
    if (!/^\s*([A-Za-z0-9_-]+(\.\w+)*@([\w-]+\.)+\w{2,3})\s*$/.test($.trim(strValue))) {
        fnErro();
         return false;
     }
     else {
         fnSucess();
         return true;
     }
     
}


//验证用户名:4到10位由英文字母，数字或下划线组成
function UserNameVate(strValue,errfn,sucfn,errfnlength,suclengfn,nullfn) {
    if ($.trim(strValue) == "$") {
       
        nullfn();
        return false;
     }
    if ($.trim(strValue).length<4|$.trim(strValue).length>10) {

     errfnlength();
     return false;
    }
    else
    {
       suclengfn();
    }
    
    if (  /^([A-Za-z0-9]|[_]){4,10}$/.test(strValue)) {

       
        sucfn();
        return false;
    }
    else {
      
        errfn();
        return true;
    }
}
//验证手机号
function MobileVate(strValue,errFn,sucFn) {
    var mobilevalue = g_langID == 2052 ? "请正确填写手机号码" : "Enter mobile number to receive latest news/reminder.";
    if (strValue != mobilevalue) {
        if (/^0?(13|15|18)[0-9]{9}$/.test(strValue)) {
            sucFn();
            return true;
        } else {
            errFn();
            return false;

        }
    }
}
//验证国际电话号
function TelephoneVate(strValue, errFn, sucFn) {
    var telvalue = g_langID == 2052 ? "请正确填写电话号码" : "Enter telephone number to receive latest news/reminder";
    //alert(strValue);
    if (strValue != telvalue) {
        if (/^[0-9\-()（）]{7,18}$/.test(strValue)) {
            sucFn();
            return true;
        } else {
            errFn();
            return false;

        }
    }
}
//验证密码格式

function PassVate(strValue,errFn,sucFn,egfn,nullfn)
{

   
    if ($.trim(strValue) == "$") {

        nullfn();
        return false;

    }
     if ($.trim(strValue).length==0) {

         egfn();
       return false;
   }
  
   if ($.trim(strValue).length < 6 | $.trim(strValue).length > 18) {

       egfn();
       
      return false;
    
}
     
  if (/^([a-z]|[0-9]|[_]){6,18}$/.test(strValue)) {
        
        sucFn();
        return true;
    }
    else {
        
        errFn();
        return false;
    }

}
//验证密码确认是否统一
function PassEqu(strValue,strConfValue,errFn,sucFn)
{

    if (strValue != strConfValue) {
        errFn();
        return false;
    }
    else {
        
        sucFn();
        return true;
    }
}

//验证邮箱是否已存在
function EmailIsExit(strValue, fn,sucfn) {

    var errs = fn;
    var suss = sucfn;
    
    $.post(
       "/GateService/RegisterManger.aspx",
       { isexemail: strValue, langid: g_langID },
       function (ise) {

           if ((+ise)) {
              suss();
           } else {
              errs();
           }

       }

       );

      
    
}
function UserNameISExist(strValue, fn,sucfn) {

var errs = fn;
var suss = sucfn;
    
    $.post(
       "/GateService/RegisterManger.aspx",
       { isusername: strValue, langid: g_langID },
       function (ise) {

           if ((+ise)) {
              suss();
           } else {
              errs();
           }

       }

       );

    

}
function getVailCode(id) {
    
    $.post(
    "/GateService/VailCode.aspx?random=" + new Date().toLocaleString(),
    {gets:"xx"},
    function (data) {
        
        $("#vailcodimg").attr("src", "/GateService/VailCode.aspx?random=" + new Date().toLocaleString());
       
    }
    );



}
//处理验证事件
function vailCode(id) {
    var newid = id;

    $.post(
    "/GateService/VailManger.aspx",
  { checkcode: $("#" + newid).val() },
   function (data) {

       if (!(+data)) {

           
           erroMsg(newid, MyProfileValidate.Verification);
           getVailCode("vailcodimg");
       } else {
           if (data == "2") {
               erroMsg(newid, MyProfileValidate.VerificationTimeout);
               
               getVailCode("vailcodimg");
           } else {
               cancleWar(newid);
               sucMsg("verCode");
           }

       }
   });
}

function InFoc(str,id) {
    if ($.trim($("#"+id).val()) == $.trim(str)) {
        $("#" + id).val("");
       } else {
    }

}
function erroMsg(id, cnMsg) {
    $("#" + id + "img").show();
    $("#" + id + "img").attr("src", "images/icon_false.gif");
    $("#"+id+"bef").show();
    document.getElementById(id+"err").innerHTML = "";
    $("#" + id + "err").show().append("<span>" + cnMsg + "</span>");
    document.getElementById(id + "img").setAttribute("src", "images/icon_false.gif");
    //document.getElementById(id).setAttribute("class", "0");
    document.getElementById(id).className = "0";
    if (id == "telephone")
        document.getElementById(id).setAttribute("class", "sno");  
    return false;
}
function sucMsg(id) {
    
    $("#" + id + "img").show();
    $("#" + id + "bef").css("display", "none");
    document.getElementById(id + "err").innerHTML = "";
    $("#" + id + "err").css("display", "none");
    document.getElementById(id + "img").setAttribute("src", "images/icon_true.gif");
    //document.getElementById(id).setAttribute("class","1");
    document.getElementById(id).className = "1";
    if (id == "telephone")
        document.getElementById(id).setAttribute("class", "sno");  
    return true;
}
function cancleWar(id) {
    $("#" + id + "img").css("display","none");
    $("#" + id + "bef").css("display", "none");
    document.getElementById(id + "err").innerHTML = "";
    $("#" + id + "err").css("display", "none");

}



function OutFoc(str,id,lastRex) {
    if ($.trim($("#" + id).val()) == "" || str == "$") {
        $("#" + id).val(str);
        //document.getElementById(id).setAttribute("class","0");
        document.getElementById(id).className = "0";
        //alert(id);
        if (id == "telephone")
            document.getElementById(id).setAttribute("class","sno");
        cancleWar(id);
        return false;
    } else {
        if (id == "txtemail") {
            EmailVate($("#" + id).val(),
            function () {

                if ($("#" + id).val()== "$" & lastRex == "1") {
                    
                    erroMsg(id, MyProfileValidate.emailNull);
                } else {
                    erroMsg(id, MyProfileValidate.emailerror);
                }



            }, function () {
                EmailIsExit(
                $("#" + id).val(),
                function () {

                    erroMsg(id, MyProfileValidate.emailexist);

                },
                function () {
                    sucMsg(id);
                }
                );


            }

            );

        }
        if (id == "txtusername") {
            UserNameVate(
            $("#" + id).val(),
            function () {
                    erroMsg(id, MyProfileValidate.username);
            }
            , function () {

                UserNameISExist(
                $("#" + id).val(),
                function () {
                    erroMsg(id, MyProfileValidate.userexist);
                }
                , function () { sucMsg(id); }
                );

            }
            , function () {
                erroMsg(id, MyProfileValidate.userlength);
            }
            , function () {
                sucMsg(id);
            }
            , function () {
                erroMsg(id, MyProfileValidate.usernameNull);
            }
            );

        }
        if (id == "mobilephone") {
            MobileVate(
            $("#" + id).val(),
            function () {
                erroMsg(id, MyProfileValidate.phonenum);
            },
            function () {
                sucMsg(id);
            }
            );

        }
        if (id == "telephone") {
            TelephoneVate(
            $("#" + id).val(),
            function () {
                erroMsg(id, MyProfileValidate.telephonenum);
            },
            function () {
                sucMsg(id);
            }
            );
        }
        if (id == "verCode") {
            vailCode(id);
        }
        if (id == "fave") {
            var cc = $.trim($("#fave").val());
            if (cc.length>0) {

                sucMsg(id);
            }

        }
        if (id == "cheM") {
            (function (strValue, fn, sucFn) {
                if (strValue != "checked") {
                    fn();

                } else {
                    sucFn();
                }
            })($("#" + id).attr("checked")
            ,
            function () {

                erroMsg(id, MyProfileValidate.Agreement);
                
            }
            ,
            function () { sucMsg(id); }
            );

        }
        

        

    }
}
var ccArr = "";
//从服务器端获取艺术形式列表
function getArtsFromServer() {
    $.post(
    "/GateService/RegisterManger.aspx",
    { getArts: "yes", langid: g_langID },
    function (data) {
        var languare = g_langID == 2052 ? '全选' : 'All';
        data = '<li><input id="checkall" onclick="checkAll(this);" type="checkbox" value="" />'+languare+'</li><li>&nbsp;</li><li>&nbsp;</li>' + data;
        $("#artList").html(data);
    }
    );
}

//全选
function checkAll(chb) {
    //$('.Arts0').attr('checked', chb.checked);
    var checklist = document.getElementsByName("artselect");
    for (var i = 0; i < checklist.length; i++) {
        checklist[i].checked = chb.checked;
        ccheck(checklist[i].id);
    }
}

// 艺术形式选定函数
function ccheck(id) {
    //alert($("#" + id).attr("checked"));
    if ($("#" + id).attr("checked") == "checked") {
        ccArr += "|" + $("#" + id).attr("value");
    }
    else {
        $('#checkall').attr("checked", false);
        ccArr = ccArr.replace("|" + $("#" + id).attr("value"), "");
    }
}

// 绑定web选定函数
var wbArr = "";
function wbcheck() {
    $(".wb0").each(function (data) {
        $("#b" + data).click(
    function () {
        if ($("#b" + data).attr("checked") == "checked") {
            wbArr += "|" + $("#b" + data).attr("value");

        } else {
            wbArr = wbArr.replace("|" + $("#b" + data).attr("value"),"");
        }
    }
    );
    });
}
//web来源x




function cc(a) {


    $('#' + a).css('display', 'none');
    $('#' + "p" + a).css('display', 'inline');
    $('#' + "p" + a).focus();

}
function passInFoc(str, id) {

    if ($.trim($("#" + id).val()) == str) {
        $("#" + id).val("");

    }
    document.getElementById(id).setAttribute("type", "password");
    
}
function passOutFoc(str, id) {
   if ( $.trim($("#" + id).val()) == "") {

      
//       document.getElementById(id).setAttribute("type", "text");
       //      $("#" + id).val(str);
       $("#"+id).css("display","none");
       $("#"+id.substring(1)).css("display","inline"); 
      cancleWar(id);

 }
   else 
   {

     if (id == "pass" )
     {
         PassVate
           (
          $("#" + id).val(),
          function () { erroMsg(id, MyProfileValidate.pwformat); },
          function () {
                 sucMsg(id); if ($.trim($("#" + id + "Conf").val())!="") {
                 PassEqu(
                         $("#pass").val(), $("#passConf").val(),
                                                         
                         function () { erroMsg(id + "Conf", MyProfileValidate.pwcoflct); } , 
                         function () { sucMsg(id + "Conf"); }
                 );
                                           
             }
         },

           function () { erroMsg(id, MyProfileValidate.pwlength); },
           function () { erroMsg(id, MyProfileValidate.passNull); }
         );
        
   }



     if (id == "passConf") {
        
   
        if($.trim($("#pass").val())!=""){
      
         PassEqu(
        $("#pass").val(),
        $("#passConf").val(),
        function () { erroMsg(id, MyProfileValidate.pwcoflct); },
        function () { sucMsg(id); }
        );
        
       
        
     }
  }      
}
 
}
                
               
             
            
 
 var headimg="";

 
 function LastRex() {

     
     var Mrexids = ["txtemail", "txtusername", "verCode", "cheM"];
     var Pssrexids = ["pass", "passConf"];
     var rexids = ["mobilephone"];
     var rexbn = true;
     for (var i = 0; i < Mrexids.length; i++)
      {

         if (!(+$("#" + Mrexids[i]).attr("class"))) 
         {
             rexbn = false;
             $("#" + Mrexids[i]).val("$");
             OutFoc("xx", Mrexids[i],"1");
             $("#" + Mrexids[i]).val("");
            
         }

         
     }
     for (var i = 0; i < Pssrexids.length; i++) {

         if (!(+$("#" + Pssrexids[i]).attr("class"))) {
             rexbn = false;
             $("#" + Pssrexids[i]).val("$");
             document.getElementById(Pssrexids[i]).setAttribute("type", "password");
             passOutFoc("xx", Pssrexids[i]);
             $("#" + Pssrexids[i]).val("");
           

         }
     }
     if ($("#mobilephoneimg").attr("src") != "images/icon_true.gif") {
         rexbn = false;
        

     }
     

     if (rexbn) {

         var birth="";
         if (document.getElementById("Y").value > 0 & document.getElementById("M").value > 0 & document.getElementById("D").value > 0) {
             birth = document.getElementById("Y").value + "/" + document.getElementById("M").value + "/" + document.getElementById("D").value;
         }
         var phonestr = $("#mobilephone").attr("class") == "1" ? $("#mobilephone").val() : "";
         var strValue=$("#telephone").val();
         var telvalue = g_langID == 2052 ? "请正确填写电话号码" : "Enter telephone number to receive latest news/reminder";
         //alert(strValue);
         if (phonestr.length > 0) {
             if (strValue != telvalue) {
                 if ( /^[0-9\-()（）]{7,18}$/ .test(strValue)) {
                     phonestr = strValue + phonestr;
                 }
             }
         }

         $.post(
         "/GateService/RegisterManger.aspx",
          {
              reg: "yes",
              email: $("#txtemail").val(),
              name: $("#txtusername").val(),
              pass: $("#pass").val(),

              phone: phonestr,
              artslist: ccArr,
              wblist: wbArr,
              fave: $("#fave").val(),
              headurl: headimg,
              birthDay: birth,
              city: document.getElementById("Provicelist").value + "/" + document.getElementById("Citylist").value,
              langid: g_langID,
              resUkemail: $("#AgreeUkEmail").attr("checked") == "checked" ? "1" : "0"
          },
          function (data) {
              if (!(+data)) {

                  alert(MyProfileValidate.registerError);
              } else {
                  alert(MyProfileValidate.registerSuccess);
                  if (g_langID == 1033) {
                      window.location = "/en/home.html";
                  }
                  else {
                      window.location = "/cn/home.html";
                  }

              }


          }
         );

     }
   

 }









